﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using System.Configuration;

using QuanLyChoVay.Entity;
using QuanLyChoVay.Bussiness;
using QuanLyChoVay.Database;

namespace QuanLyChoVay
{
    public partial class QuanLyNhanVien : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter sqlda;
        DataSet ds;
        //DataTable dt;
        SqlCommandBuilder cmb;
        public QuanLyNhanVien()
        {
            InitializeComponent();
        }

        private void QuanLyNhanVien_Load(object sender, EventArgs e)
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            cmd = new SqlCommand("SELECT * FROM NhanVien", conn);
            conn.Open();
            sqlda = new SqlDataAdapter(cmd);
            ds = new DataSet();
            sqlda.Fill(ds, "NhanVien");
            cmb = new SqlCommandBuilder(sqlda);
            //MessageBox.Show(cmb.GetDeleteCommand().CommandText);
            //MessageBox.Show(cmb.GetInsertCommand().CommandText);
            //MessageBox.Show(cmb.GetUpdateCommand().CommandText);
            dataGridView1.DataSource = ds.Tables["NhanVien"];
            conn.Close();
            //NhanVienBuss nvb = new NhanVienBuss();
            //DataTable dt = new DataTable();
            //dt = nvb.select_NhanVien();

            //dataGridView1.DataSource = dt;
            btnluulai.Visible = false;
            btnlamlai.Visible = false;
            btnquaylai.Visible = false;
            btncapnhat.Visible = false;
            btnthoatcapnhat.Visible = false;
            groupBox2.Enabled = false;
        }

        private void btnthemmoi_Click(object sender, EventArgs e)
        {
            btnluulai.Visible = true;
            btnlamlai.Visible = true;
            btnquaylai.Visible = true;
            btnthemmoi.Visible = false;
            btnsua.Visible = false;
            btnxoa.Visible = false;
            btncapnhat.Visible = false;
            btnthoatcapnhat.Visible = false;

            groupBox2.Enabled = true;

            txtmanv.Text = "";
            txttendn.Text = "";
            txtmatkhau.Text = "";
            txthoten.Text = "";
            txtemail.Text = "";
            txtdiachi.Text = "";
            txtsdt.Text = "";
            txtquyen.Text = "";

            txtmanv.Focus();

            dataGridView1.Enabled = false;

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát", "Xác nhận thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Dispose();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            int j = dataGridView1.ColumnCount;
            //MessageBox.Show(j.ToString());
            //MessageBox.Show(i.ToString());
            txtmanv.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            txttendn.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            txtmatkhau.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
            txthoten.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            txtemail.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            dateTimePicker1.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            txtdiachi.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
            txtsdt.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
            txtquyen.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
        }

        private void btnluulai_Click(object sender, EventArgs e)
        {
            NhanVien nv = new NhanVien();
            nv.Manv = txtmanv.Text;
            nv.Tendn = txttendn.Text;
            nv.Matkhau = txtmatkhau.Text;
            nv.Hoten = txthoten.Text;
            nv.Email = txtemail.Text;
            nv.Ngaysinh = dateTimePicker1.Text;
            nv.Diachi = txtdiachi.Text;
            nv.Sdt = txtsdt.Text;
            nv.Quyen = Convert.ToInt16(txtquyen.Text);

            NhanVienBuss nvb = new NhanVienBuss();
            try
            {
                nvb.insertnhanvien(nv);
                MessageBox.Show("Thêm mới thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //QuanLyNhanVien qlnv = new QuanLyNhanVien();
                //qlnv.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra khi thực hiện insert");
            }
        }

        private void btnlamlai_Click(object sender, EventArgs e)
        {
            txtmanv.Text = "";
            txttendn.Text = "";
            txtmatkhau.Text = "";
            txthoten.Text = "";
            txtemail.Text = "";
            txtdiachi.Text = "";
            txtsdt.Text = "";
            txtquyen.Text = "";
            txtmanv.Focus();
        }

        private void btnquaylai_Click(object sender, EventArgs e)
        {
            this.Hide();
            QuanLyNhanVien qlnv = new QuanLyNhanVien();
            qlnv.ShowDialog();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            //xoa thu cong vay mai (26_05) lam-----------
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            //dataGridView1.DataSource = dt;
            //update from dataset to database
            sqlda.Update(ds, "NhanVien");
            MessageBox.Show("Deleted");
            dataGridView1.DataSource = ds.Tables[0];
            //MessageBox.Show(ds.Tables[0].DefaultView.Count.ToString());
            //this.Hide();
            //QuanLyNhanVien qlnv = new QuanLyNhanVien();
            //qlnv.ShowDialog();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            int i = -1;
            i = dataGridView1.CurrentRow.Index;
            if (i >= 0)
            {
                txtmanv.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
                txttendn.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
                txtmatkhau.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
                txthoten.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
                txtemail.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
                dateTimePicker1.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
                txtdiachi.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
                txtsdt.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
                txtquyen.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn nhân viên để thay đôi thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            btnthemmoi.Visible = false;
            btnxoa.Visible = false;
            btnsua.Visible = false;
            groupBox2.Enabled = true;
            txtmanv.Enabled = false;
            txttendn.Enabled = false;
            btncapnhat.Visible = true;
            btnthoatcapnhat.Visible = true;
            txtmatkhau.Focus();
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            NhanVien nv = new NhanVien();
            nv.Manv = txtmanv.Text;
            nv.Tendn = txttendn.Text;
            nv.Matkhau = txtmatkhau.Text;
            nv.Hoten = txthoten.Text;
            nv.Email = txtemail.Text;
            nv.Ngaysinh = dateTimePicker1.Text;
            nv.Diachi = txtdiachi.Text;
            nv.Sdt = txtsdt.Text;
            nv.Quyen = Convert.ToInt16(txtquyen.Text);

            NhanVienBuss nvb = new NhanVienBuss();
            try
            {
                nvb.update_NhanVien(nv);
                MessageBox.Show("Cập nhật thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra khi thực hiện update");
            }
            this.Hide();
            QuanLyNhanVien qlnv = new QuanLyNhanVien();
            qlnv.ShowDialog();
        }

        private void btnthoatcapnhat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát cập nhật", "Xác nhận thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                QuanLyNhanVien qlnv = new QuanLyNhanVien();
                qlnv.ShowDialog();
            }
        }
    }
}
